#!/usr/bin/python
# -*- coding:utf-8 -*-

import mongoengine as db

from app.db import BaseDocument


class ApiAuthChoices(object):
    # 认证方式
    AUTH_TYPE = (
        ("api_key", "api_key"),  # 目前只支持在header自定义key， value
        ("basic_auth", "basic_auth"),  # 使用basic auth 方式
        ("no_auth", "no_auth"),  # 不用认证
    )
    ADD_TO = (
        ("header", "header"),
        ("basic_auth", "basic_auth"),
        ("no_auth", "no_auth")
    )
    METHOD = (
        ("GET", "GET"),
        ("POST", "POST"),
    )


class OutPutConfig(BaseDocument):
    """
    推送url配置
    """

    uid = db.ObjectIdField(required=True)
    desc = db.StringField(max_length=200, default="")
    # 推送url， 目前只支持http推送
    url = db.StringField(blank=True, max_length=512)
    auth_type = db.StringField(default="no_auth", choices=ApiAuthChoices.AUTH_TYPE)
    add_to = db.StringField(default="header", choices=ApiAuthChoices.ADD_TO)
    auth_key = db.StringField(default="")
    auth_value = db.StringField(default="")
    method = db.StringField(default="POST", choices=ApiAuthChoices.METHOD)
    push_task_types = db.ListField(default=[])
    is_active = db.BooleanField(default=True)
    created_at = db.DateTimeField(blank=True)
    updated_at = db.DateTimeField(blank=True)
    meta = {
        'indexes': [
            'uid',
        ],
        'ordering': ['-created_at'],
        'allow_inheritance': True,
        'collection': "out_put_configs",
    }

    def __str__(self):
        return self.url

    def __repr__(self):
        return self.__str__()


class ApiToken(BaseDocument):
    """
    二次开发输入API token
    """

    uid = db.ObjectIdField(required=True)
    token = db.StringField(required=True, unique=True)
    is_active = db.BooleanField(default=True)
    expired_time = db.DateTimeField(blank=True)
    created_at = db.DateTimeField(blank=True)
    updated_at = db.DateTimeField(blank=True)
    desc = db.StringField(max_length=200, default="")
    meta = {
        'indexes': [
            'uid',
        ],
        'ordering': ['-created_at'],
        'allow_inheritance': True,
        'collection': "api_tokens",
    }

    def __str__(self):
        return self.token

    def __repr__(self):
        return self.__str__()
